package org.musicPlayerServer.server;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.musicPlayerServer.business.Controller;
import org.musicPlayerServer.business.object.Content;
import org.musicPlayerServer.business.object.Music;
import org.musicPlayerServer.gui.GuiManager;


public class MainHandler extends AbstractHandler    {

    Log log = LogFactory.getLog(getClass());




    public void handle(String target,Request baseRequest,HttpServletRequest request,HttpServletResponse response) 
    throws IOException, ServletException {

	Controller c = Controller.getInstance();
	response.setContentType("text/html;charset=utf-8");
	response.setStatus(HttpServletResponse.SC_OK);
	baseRequest.setHandled(true);
	String action = request.getParameter("action");
	log.info("Request: action="+action);
	GuiManager.getInstance().init();
	response.getWriter().println(GuiManager.getInstance().getHead());
	String servletPath = "http://"+request.getServerName()+":"+request.getServerPort()+"/"+request.getServletPath();
	
	try {
	    if(action==null){
		
	    }else if(action.equals("play")){
		String file = request.getParameter("file");
		File f = new File(file);
		log.info("  file="+file);

		Controller.getInstance().stop();
		Controller.getInstance().select(new Music(file));
		Controller.getInstance().play();

		response.getWriter().println("<h2>Playing "+Controller.getInstance().getCurrent()+"</h2>");	   
	    }else if(action.equals("pause")){
		Controller.getInstance().pause();
	    }else if(action.equals("resume")){
		Controller.getInstance().resume();
	    }else if(action.equals("next")){
		Controller.getInstance().next();
	    }else if(action.equals("list")){
		String folder = request.getParameter("folder");
		log.info("  folder="+folder);
		c.moveToFolder(folder);
	    }
	    
	    displayCurrentFolder(response, c, servletPath);

	} catch (Exception e) {
	    e.printStackTrace();
	    response.getWriter().println("Error: "+e.getMessage()+" ");
	    log.error(e);
	}
	response.getWriter().println(GuiManager.getInstance().getTail());
    }

    private void displayCurrentFolder(HttpServletResponse response,
	    Controller c, String servletPath) throws IOException {
	File current = c.getCurrentFolder().getFile();
	response.getWriter().println("<a href=\""+servletPath+"?action=list&folder=parent\">..</a>");
	response.getWriter().println("<h1>"+current.getName()+"</h1>");
	response.getWriter().println("<h2>("+current.getParent()+")</h2><br/>");
	response.getWriter().println();
	for(Content content : c.getContent()){
	    //log.info("  content="+content);
	    response.getWriter().println(content.toHtml(servletPath));
	    response.getWriter().println("<br/>");

	}
    }
    

}

